package yaner.hotel.manager.resource.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yaner.hotel.manager.common.constant.Constants;
import yaner.hotel.manager.common.dto.user.UserAddDto;
import yaner.hotel.manager.common.dto.user.UserDto;
import yaner.hotel.manager.common.dto.user.UserUpdateDto;
import yaner.hotel.manager.common.enums.ResponseCodeEnums;
import yaner.hotel.manager.common.exception.AppException;
import yaner.hotel.manager.common.vo.UserVo;
import yaner.hotel.manager.resource.dao.IUserDao;
import yaner.hotel.manager.resource.entity.User;
import yaner.hotel.manager.resource.mapper.IUserMapper;
import yaner.hotel.manager.resource.service.IUserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements IUserService {
    private final IUserDao userDao;
    private final IUserMapper userMapper;
    public UserServiceImpl(IUserDao userDao, IUserMapper userMapper) {
        this.userDao = userDao;
        this.userMapper = userMapper;
    }

    @Override
    public List<UserVo> userList(UserDto userDto) {
        List<UserVo> userVoList = new ArrayList<>();
        List<User> userList = userDao.selectList(
                new QueryWrapper<User>().lambda().orderByDesc(User::getCreateTime)
        );
        userList.forEach(user -> {
            UserVo userVo = userMapper.toVo(user);
            userVoList.add(userVo);
        });
        return userVoList;
    }

    @Override
    public UserVo getUserInfo(String userId) {
        User user = userDao.selectById(userId);
        if (Objects.isNull(user)) {
            throw new AppException(ResponseCodeEnums.USER_NOT_EXIST.getInfo());
        }
        return userMapper.toVo(user);
    }

    @Override
    public List<UserVo> checkUserData(String loginAccount) {
        List<UserVo> userVoList = new ArrayList<>();
        List<User> userList = userDao.selectList(
                new QueryWrapper<User>().lambda()
                        .eq(User::getStatus, "1")
                        .eq(User::getAccount, loginAccount)
        );
        userList.forEach(user -> {
            UserVo userVo = userMapper.toVo(user);
            userVoList.add(userVo);
        });
        return userVoList;
    }

    @Override
    public String addUserInfo(UserAddDto userAddDto) {
        User user = userMapper.toAddEntity(userAddDto);
        int i = userDao.insert(user);
        return String.valueOf(i);
    }

    @Override
    public String updateUserInfo(UserUpdateDto userUpdateDto) {
        User user = userMapper.toUpdateEntity(userUpdateDto);
        user.setUserId(userUpdateDto.getUserId());
        int i = userDao.updateById(user);
        return String.valueOf(i);
    }

    @Override
    public String deleteUserInfo(String userId) {
        int i = userDao.deleteById(userId);
        return String.valueOf(i);
    }

    @Override
    public String updateUserPassword(UserUpdateDto userUpdateDto) {
        Objects.requireNonNull(userUpdateDto.getUserId(), "userId is null");
        getUserInfo(userUpdateDto.getUserId());
        if ("-1".equals(userUpdateDto.getPassword())) {
            String md5Hex = DigestUtil.md5Hex(Constants.INIT_PASSWORD);
            userUpdateDto.setPassword(md5Hex);
        }
        String md5Hex = DigestUtil.md5Hex(userUpdateDto.getPassword());
        userUpdateDto.setPassword(md5Hex);
        User user = userMapper.toUpdateEntity(userUpdateDto);
        user.setUserId(userUpdateDto.getUserId());
        int i = userDao.updateById(user);
        return String.valueOf(i);
    }

    @Override
    public String updateUserStatus(UserUpdateDto userUpdateDto) {
        Objects.requireNonNull(userUpdateDto.getUserId(), "userId is null");
        Objects.requireNonNull(userUpdateDto.getStatus(), "status is null");
        getUserInfo(userUpdateDto.getUserId());
        User user = userMapper.toUpdateEntity(userUpdateDto);
        user.setUserId(userUpdateDto.getUserId());
        int i = userDao.updateById(user);
        return String.valueOf(i);
    }
}
